<?php

namespace App;

use App\Models\Clue;
use App\Models\Timetable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasRoles;
    //use HasPermissions;
    use Notifiable;


    protected $guard_name = 'api'; // 使用任何你想要的守卫


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'password', 'username', 'status', 'phone'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }


    /**
     * fix Laravel-Permission `call undefined method getPermissionNames ` bug.
     */
//    public function getPermissionNames()
//    {
//        return $this->permissions->pluck('name');
//    }

//    public function permissions()
//    {
//        return $this->morphToMany(Permission::class, 'model', 'model_has_permissions');
//    }

    public function clues()
    {
        return $this->belongsToMany(Clue::class, 'clue_distributions', 'user_id', 'clue_id');
    }

    public function works()
    {
        return $this->belongsToMany(Timetable::class, 'works', 'user_id', 'timetable_id');
    }

    public function workDate($date)
    {
        return $this->works()->whereDate('date', $date);
    }


}
